package com.hra.camera2demo;

import android.hardware.camera2.CameraCharacteristics;
import android.hardware.camera2.CaptureRequest;
import android.hardware.camera2.params.StreamConfigurationMap;
import android.util.Size;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Locale;

public class Camera2ConfigHelper {
    public static String[] getFocusModeNames(int[] focusModes) {
        List<String> names = new ArrayList<>();
        for (int mode : focusModes) {
            switch (mode) {
                case CameraCharacteristics.CONTROL_AF_MODE_OFF:
                    names.add("关闭对焦");
                    break;
                case CaptureRequest.CONTROL_AF_MODE_AUTO:
                    names.add("点击自动对焦");
                    break;
                case CaptureRequest.CONTROL_AF_MODE_MACRO:
                    names.add("微距模式");
                    break;
                case CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_VIDEO:
                    names.add("连续对焦（视频）");
                    break;
                case CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE:
                    names.add("连续对焦（拍照）");
                    break;
                case CameraCharacteristics.CONTROL_AF_MODE_EDOF:
                    names.add("数字聚焦");
                    break;
            }
        }
        return names.toArray(new String[0]);
    }

    public static String[] getWhiteBalanceModeNames(int[] whiteBalanceModes) {
        List<String> names = new ArrayList<>();
        for (int mode : whiteBalanceModes) {
            switch (mode) {
                case CaptureRequest.CONTROL_AWB_MODE_OFF:
                    names.add("关闭");
                    break;
                case CaptureRequest.CONTROL_AWB_MODE_AUTO:
                    names.add("自动");
                    break;
                case CaptureRequest.CONTROL_AWB_MODE_INCANDESCENT:
                    names.add("白炽光");
                    break;
                case CaptureRequest.CONTROL_AWB_MODE_FLUORESCENT:
                    names.add("荧光");
                    break;
                case CaptureRequest.CONTROL_AWB_MODE_WARM_FLUORESCENT:
                    names.add("暖荧光");
                    break;
                case CaptureRequest.CONTROL_AWB_MODE_DAYLIGHT:
                    names.add("日光");
                    break;
                case CaptureRequest.CONTROL_AWB_MODE_CLOUDY_DAYLIGHT:
                    names.add("阴天");
                    break;
                case CaptureRequest.CONTROL_AWB_MODE_TWILIGHT:
                    names.add("微光（黄昏）");
                    break;
                case CaptureRequest.CONTROL_AWB_MODE_SHADE:
                    names.add("阴影");
                    break;
            }
        }
        return names.toArray(new String[0]);
    }

    public static String[] getSceneModeNames(int[] sceneModes) {
        List<String> names = new ArrayList<>();
        for (int mode : sceneModes) {
            switch (mode) {
                case CaptureRequest.CONTROL_SCENE_MODE_DISABLED:
                    names.add("关闭");
                    break;
                case CaptureRequest.CONTROL_SCENE_MODE_FACE_PRIORITY:
                    names.add("自动");
                    break;
                case CaptureRequest.CONTROL_SCENE_MODE_ACTION:
                    names.add("运动");
                    break;
                case CaptureRequest.CONTROL_SCENE_MODE_PORTRAIT:
                    names.add("人像");
                    break;
                case CaptureRequest.CONTROL_SCENE_MODE_LANDSCAPE:
                    names.add("风景");
                    break;
                case CaptureRequest.CONTROL_SCENE_MODE_NIGHT:
                    names.add("夜景");
                    break;
                case CaptureRequest.CONTROL_SCENE_MODE_NIGHT_PORTRAIT:
                    names.add("夜景人像");
                    break;
                case CaptureRequest.CONTROL_SCENE_MODE_THEATRE:
                    names.add("剧场");
                    break;
                case CaptureRequest.CONTROL_SCENE_MODE_BEACH:
                    names.add("沙滩");
                    break;
                case CaptureRequest.CONTROL_SCENE_MODE_SNOW:
                    names.add("雪景");
                    break;
                case CaptureRequest.CONTROL_SCENE_MODE_SUNSET:
                    names.add("日落");
                    break;
                case CaptureRequest.CONTROL_SCENE_MODE_STEADYPHOTO:
                    names.add("防抖");
                    break;
                case CaptureRequest.CONTROL_SCENE_MODE_FIREWORKS:
                    names.add("烟火");
                    break;
                case CaptureRequest.CONTROL_SCENE_MODE_SPORTS:
                    names.add("运动追踪");
                    break;
                case CaptureRequest.CONTROL_SCENE_MODE_PARTY:
                    names.add("派对");
                    break;
                case CaptureRequest.CONTROL_SCENE_MODE_CANDLELIGHT:
                    names.add("烛光");
                    break;
                case CaptureRequest.CONTROL_SCENE_MODE_BARCODE:
                    names.add("条码扫描");
                    break;
                case CaptureRequest.CONTROL_SCENE_MODE_HIGH_SPEED_VIDEO:
                    names.add("高速视频");
                    break;
            }
        }
        return names.toArray(new String[0]);
    }

    public static String[] getISOModeNames(int[] isoModes) {
        List<String> names = new ArrayList<>();
        for (int iso : isoModes) {
            if (iso == 0) {
                names.add("自动");
            } else {
                names.add(String.valueOf(iso));
            }
        }
        return names.toArray(new String[0]);
    }

    public static String[] getExposureTimeNames(long[] exposureValues) {
        List<String> names = new ArrayList<>();
        for (long value : exposureValues) {
            if (value == 0) {
                names.add("自动");
            } else {
                // 将纳秒转换为秒（例如 10,000,000 ns -> "1/100秒"）
                double seconds = value / 1000000000.0;
                String displayText;
                if (seconds < 1.0) {
                    displayText = "1/" + (int) (1.0 / seconds) + "秒";
                } else {
                    displayText = String.format(Locale.getDefault(), "%.1f秒", seconds);
                }
                names.add(displayText);
            }
        }
        return names.toArray(new String[0]);
    }

    public static List<Size> getSupportedPictureSize(StreamConfigurationMap s, int format) {
        if (s == null) {
            return null;
        }
        List<Size> supportedValues = new ArrayList<>();
        Size[] highSizes = s.getHighResolutionOutputSizes(format);
        if (highSizes != null) {
            Collections.addAll(supportedValues, highSizes);
        }
        Size[] sizes = s.getOutputSizes(format);
        if (sizes != null) {
            Collections.addAll(supportedValues, sizes);
        }
        return supportedValues;
    }

    public static String[] getSizeStrings(List<Size> sizes) {
        String[] sizeStrings = new String[sizes.size()];
        for (int i = 0; i < sizes.size(); i++) {
            Size size = sizes.get(i);
            sizeStrings[i] = size.toString();
        }
        return sizeStrings;
    }

    public static int getIndex(long[] array, long value) {
        if (array == null) return 0;
        for (int i = 0; i < array.length; i++) {
            if (array[i] == value) return i;
        }
        return 0;
    }

    public static int getIndex(int[] array, int value) {
        if (array == null) return 0;
        for (int i = 0; i < array.length; i++) {
            if (array[i] == value) return i;
        }
        return 0;
    }

    public static int getIndex(List<Size> list, Size value) {
        if (list == null || value == null) return 0;
        for (int i = 0; i < list.size(); i++) {
            if (value.equals(list.get(i))) return i;
        }
        return 0;
    }

    public static int[] addIfNotPresent(int[] array, int target) {
        if (array == null) {
            return new int[]{target};
        }
        for (int num : array) {
            if (num == target) {
                return array;
            }
        }
        // 创建新数组（长度+1）
        int[] newArray = new int[array.length + 1];
        // 将新元素放在第一位
        newArray[0] = target;
        // 插入目标值到开头
        System.arraycopy(array, 0, newArray, 1, array.length);
        return newArray;
    }

    static class CompareSizesByArea implements Comparator<Size> {
        @Override
        public int compare(Size lhs, Size rhs) {
            return Long.signum((long) rhs.getWidth() * rhs.getHeight() - (long) lhs.getWidth() * lhs.getHeight());
        }
    }
}
